package erfgame.frontend.desktop.view.terrain.iso.interaction;

import erfgame.core.Point3D;
import erfgame.core.dao.ObjectDAO;
import erfgame.core.entity.Entity;
import erfgame.core.entity.EntityByTypeFactory;
import erfgame.core.entity.EntityType;
import erfgame.core.entity.Physical;
import erfgame.core.world.World;
import erfgame.core.world.WorldDAOManager;
import erfgame.core.world.terrain.BlockEntityPositions;
import erfgame.core.world.terrain.TerrainUtils;

import erfgame.frontend.desktop.view.terrain.iso.IsometricTerrainView;
import erfgame.frontend.desktop.view.terrain.iso.handler.InteractionHandler;

public class EntityDroppingInteraction implements Interaction {
	
	private Point3D position;
	private IsometricTerrainView view;
	private WorldDAOManager worldDAOManager;
	private EntityType entityType;
	
	private EntityByTypeFactory entityFactory;
	
	public EntityDroppingInteraction( 
			Point3D position,
			EntityType entityType,
			EntityByTypeFactory entityFactory,
			IsometricTerrainView view, 
			WorldDAOManager worldDAOManager
	) {
		this.position = position;
		this.view = view;
		this.worldDAOManager = worldDAOManager;
		this.entityType = entityType;
		this.entityFactory = entityFactory;
	}
	
	public InteractionHandler activate() {
		World world = view.getCurrentWorld();
		Entity entity = entityFactory.createEntity(world, entityType);
		Physical physical = (Physical)entity;
		physical.setPosition( new Point3D( position.x, position.y, position.z ) );

		ObjectDAO<Long, Entity> entityDAO = worldDAOManager.getEntityDAO(world);
		ObjectDAO<Point3D, BlockEntityPositions> entityPositionsDAO = worldDAOManager.getBlockEntityPositionsDAO(world);
		
		entityDAO.storeObject( entity.getId(), entity );
		TerrainUtils.addEntity(
				world,
				entity, 
				entityPositionsDAO,
				entityDAO, 
				view
		);
		return null;
	}
}
